/*  This file is part of -_-.

    -_- is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    -_- is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with -_-.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2012-2013 Oliver Katz */

#include "excstack.h"

z_excframe z_excframe_init(char *f, int r, char *d)
{
    z_excframe tmp;
    tmp.fname = f;
    tmp.rc = r;
    tmp.rd = d;
    return tmp;
}

int z_excframe_release(z_excframe f)
{
    return 0;
}

char *z_excframe_function_get(z_excframe *f)
{
    return f->fname;
}

int z_excframe_returncode_get(z_excframe *f)
{
    return f->rc;
}

char *z_excframe_description_get(z_excframe *f)
{
    return f->rd;
}

int z_excframe_dump(z_excframe *f)
{
    printf("%s: %s (%i)\n", f->fname, f->rd, f->rc);
    return 0;
}

z_excframe *_z_excstack;
z_excframe *_z_exchead;

int z_excstack_init()
{
    _z_excstack = malloc(sizeof(z_excframe)*Z_EXCSTACK_SIZE);
    memset(_z_excstack, 0, sizeof(z_excframe)*Z_EXCSTACK_SIZE);
    _z_exchead = _z_excstack;
    return 0;
}

int z_excstack_push(z_excframe f)
{
    if (_z_exchead-_z_excstack >= Z_EXCSTACK_SIZE)
        return 1;
    *_z_exchead++ = f;
    return 0;
}

int z_excstack_dump()
{
    printf("fatal error - stacktrace\n");
    for (z_excframe *i = _z_excstack; i < _z_exchead; i++)
        z_excstack_dump(i);
    return 0;
}
